/**
 * 
 */
package com.estock.common.model.security;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.estock.common.util.CommonUtil;
import com.estock.common.util.ESUserUtil;

/**
 * @author richard
 * 
 */
@MappedSuperclass
public class BaseEntityWithoutID implements Serializable {

	private static final long serialVersionUID = -2309221826592443194L;

	private String whoCreated;
	private String whoUpdated;
	private Date dateCreated;
	private Date dateUpdated;

	public String getWhoCreated() {
		return whoCreated;
	}

	public void setWhoCreated(String whoCreated) {
		this.whoCreated = whoCreated;
	}

	public String getWhoUpdated() {
		return whoUpdated;
	}

	public void setWhoUpdated(String whoUpdated) {
		this.whoUpdated = whoUpdated;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public Date getDateUpdated() {
		return dateUpdated;
	}

	public void setDateUpdated(Date dateUpdated) {
		this.dateUpdated = dateUpdated;
	}

	public void setCreationSystemColumns() {
		String userName = getUserName();
		Date now = new Date();
		setWhoCreated(userName);
		setDateCreated(now);
		setWhoUpdated(userName);
		setDateUpdated(now);
	}

	public void setUpdateSystemColumns() {
		Date now = new Date();
		setWhoUpdated(getUserName());
		setDateUpdated(now);
	}
	
	@Transient
	private String getUserName() {
		String userName = "admin";
    	if (!CommonUtil.isUnitTest) {
    		userName = ESUserUtil.getCurrentUserName();
    	}
		return userName;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
